package com.projectsexception.festivales.setup.xml;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.res.XmlResourceParser;

import com.projectsexception.festivales.setup.FeedInfo;
import com.projectsexception.festivales.setup.MapInfo;
import com.projectsexception.festivales.setup.ShowInfo;

public class SetupParser {
    
    private <T> List<T> parse(XmlResourceParser res, XMLHandler<T> xmlHandler) {
        try {
            xmlHandler.startDocument();
            while (res.getEventType() != XmlResourceParser.END_DOCUMENT) {
                if (res.getEventType() == XmlResourceParser.START_TAG) {
                    xmlHandler.startTag(res);
                } else if (res.getEventType() == XmlResourceParser.TEXT) {
                    xmlHandler.text(res);
                } else if (res.getEventType() == XmlResourceParser.END_TAG) {
                    xmlHandler.endTag(res);
                }
                res.next();
            }
            xmlHandler.endDocument();
        } catch (XmlPullParserException ex) {
            
        } catch (IOException ex) {
            
        } finally {
            res.close();
        }
        return xmlHandler.getResult();
    }
    
    public List<FeedInfo> parseFeeds(XmlResourceParser res) {
        XMLHandler<FeedInfo> xmlHandler = new XMLHandlerFeedInfo();
        return parse(res, xmlHandler);
    }
    
    public List<MapInfo> parseMaps(XmlResourceParser res) {
        XMLHandler<MapInfo> xmlHandler = new XMLHandlerMapInfo();
        return parse(res, xmlHandler);
    }
    
    public List<ShowInfo> parseShows(XmlResourceParser res) {
        XMLHandler<ShowInfo> xmlHandler = new XMLHandlerShowInfo();
        return parse(res, xmlHandler);
    }

}
